/*
 * MainWindowPresenter.cpp
 *
 *  Created on: May 14, 2012
 *      Author: walther
 */

#include "MainWindowPresenter.h"
#include "MainWindow.h"
#include "Scene.h"
#include "Rt4FileReader.h"
#include <gdk/gdkkeysyms.h>

MainWindowPresenter::MainWindowPresenter()
{
    _image = NULL;
    _scene  = NULL;
}



MainWindowPresenter::~MainWindowPresenter()
{
    if (_scene)
        delete _scene;
    
    if (_image)
        imgDestroy(_image);
}



Image* MainWindowPresenter::getImage()
{
    return _image;
}



bool MainWindowPresenter::buildScene( std::string filename )
{
    if( _scene ) delete _scene;
    
    _scene = new Scene();
    
    Rt4FileReader reader;
    reader.loadScene( filename, _scene );
    _scene->buildBoundingBox();

    if (_image)
        imgClear( _image, 0.0f, 0.0f, 0.0f );
    
    return true;
}



void MainWindowPresenter::renderScene()
{
    if (!_scene)
        return;
    
    if (_image)
        imgDestroy( _image );
    
    _image = _scene->render();
}



void MainWindowPresenter::toggleAmbient( bool isActive )
{
    if (!_scene)
        return;
    
    _scene->toggleAmbient( isActive );
}
    


void MainWindowPresenter::toggleDiffuse( bool isActive )
{
    if (!_scene)
        return;
    
    _scene->toggleDiffuse( isActive );
}



void MainWindowPresenter::toggleSpecular( bool isActive )
{
    if (!_scene)
        return;
    
    _scene->toggleSpecular( isActive );
}
    


void MainWindowPresenter::toggleShadow( bool isActive )
{
    if (!_scene)
        return;
    
     _scene->toggleShadow( isActive );
}



void MainWindowPresenter::toggleSoftShadow( bool isActive )
{
    if (!_scene)
        return;
    
     _scene->toggleSoftShadow( isActive );
}



void MainWindowPresenter::toggleReflection( bool isActive )
{
    if (!_scene)
        return;
    
    _scene->toggleReflection( isActive );
}



void MainWindowPresenter::toggleAntiAlias( bool isActive )
{
    if (!_scene)
        return;
    
    _scene->toggleAntiAlias( isActive );
}



void MainWindowPresenter::toggleRegularGrid( bool isActive )
{
    if (!_scene)
        return;
    
    if (isActive)
        _scene->setSpaceSubdivisionType( Scene::REGULAR_GRID );
    else
        _scene->setSpaceSubdivisionType( Scene::NONE );
}



void MainWindowPresenter::rebuildGrid( int nx, int ny, int nz )
{
    if (!_scene) return;
    
    _scene->rebuildGrid( nx, ny, nz );
}



void MainWindowPresenter::setRayMemorySize( int size )
{
    if (!_scene) return;
    
    _scene->setRayMemorySize( size );
}



bool MainWindowPresenter::treatKeyPress( guint keyval )
{
    if (!_scene)
        return false;
    
    Box* aabb = _scene->getBoundingBox();
    
    Vector4D min = aabb->getMin();
    Vector4D max = aabb->getMax();
    
    double deltaX = max.x - min.x;
    double deltaY = max.y - min.y;
    double deltaZ = max.z - min.z;
    
    double distance = 1.0;
    
    if (deltaX > deltaY)
    {
        if (deltaX > deltaZ)
            distance = deltaX;
        else
            distance = deltaZ;
    }
    else
    {
        if (deltaY > deltaZ)
            distance = deltaY;
        else
            distance = deltaZ;
    }
    
    distance *= 0.1;
    
    switch( keyval )
    {
        case GDK_w:
            _scene->getCamera()->moveForward( distance );
            break;
            
        case GDK_s:
            _scene->getCamera()->moveForward( -distance );
            break;
            
        case GDK_a:
            _scene->getCamera()->moveSideways( -distance );
            break;
            
        case GDK_d:
            _scene->getCamera()->moveSideways( distance );
            break;
            
        default:
            return false;
            break;
    }
    
    return true;
}

